//
//  NSString+CLMD5.m
//  Rippton_iphone
//
//  Created by yeyongping on 2017/11/8.
//  Copyright © 2017年 mac. All rights reserved.
//

#import "NSString+CLMD5.h"
#import <CommonCrypto/CommonCrypto.h>
@implementation NSString (CLMD5)
- (NSString *)MD5Str{
    
    // 1.对MD5的加密 必须输对C语言字符串 所以我们要先将OC字符串 转化成C语言字符串
    const char *strUTF = self.UTF8String;
    
    // 2.创建字符串数组接收MD5的值
    unsigned char result[CC_MD5_DIGEST_LENGTH];
    
    // 3.计算MD5值 最终字符串通过MD5加密的值 会被存储到result数组当中
    CC_MD5(strUTF, (CC_LONG)strlen(strUTF), result);
    
    // 4.定义一个字符串 通过拼接 接收MD5加密之后的值
    NSMutableString *MD5String = [NSMutableString string];
    
    // 5.通过循环 对MD5加密的值 进行逐个字符拼接到字符串中
    for (int i = 0; i < CC_MD5_DIGEST_LENGTH; i++)
    {
        [MD5String appendFormat:@"%02X", result[i]];
    }
    return MD5String.lowercaseString;
}
- (BOOL)isSpaceString{
   
    if(([[self stringByTrimmingCharactersInSet:[NSCharacterSet whitespaceAndNewlineCharacterSet]] length]) == 0){
        return YES;
    }
    return NO;
}
/**
 去除首尾空格的字符串
 */
- (NSString *)cl_removeSpaceString{
    // 去除首尾空格
    NSString *str = [self stringByTrimmingCharactersInSet:[NSCharacterSet whitespaceAndNewlineCharacterSet]]; //去除掉首尾的空白字符和换行字符
    str = [str stringByReplacingOccurrencesOfString:@"\r" withString:@""];
//    str = [str stringByReplacingOccurrencesOfString:@"\n" withString:@""];
    return str;
    //
}




- (NSString *)xh_filterHtmlString{
    
    NSArray *components = [self componentsSeparatedByCharactersInSet:[NSCharacterSet characterSetWithCharactersInString:@"<>"]];
    
    NSMutableArray *componentsToKeep = [NSMutableArray array];
    
    for (int i = 0; i < [components count]; i = i + 2) {
        
        [componentsToKeep addObject:[components objectAtIndex:i]];
        
    }
    
    NSString *plainText = [componentsToKeep componentsJoinedByString:@""];
    
    return [plainText stringByReplacingOccurrencesOfString:@"&nbsp;" withString:@" "];;
}


+ (NSMutableArray *)getRangeStr:(NSString *)text findText:(NSString *)findText{
    NSMutableArray *arrayRanges = [NSMutableArray arrayWithCapacity:20];

    if (findText == nil && [findText isEqualToString:@""]) {
        return nil;
    }
    NSRange rang = [text rangeOfString:findText]; //获取第一次出现的range
    if (rang.location != NSNotFound && rang.length != 0) {
        [arrayRanges addObject:[NSNumber numberWithInteger:rang.location]];//将第一次的加入到数组中
        NSRange rang1 = {0,0};
        NSInteger location = 0;
        NSInteger length = 0;
        for (int i = 0;; i++){
            if (0 == i) {//去掉这个xxx
                location = rang.location + rang.length;
                length = text.length - rang.location - rang.length;
                rang1 = NSMakeRange(location, length);
            }else{
                location = rang1.location + rang1.length;
                length = text.length - rang1.location - rang1.length;
                rang1 = NSMakeRange(location, length);
            }
            //在一个range范围内查找另一个字符串的range
            rang1 = [text rangeOfString:findText options:NSCaseInsensitiveSearch range:rang1];
            if (rang1.location == NSNotFound && rang1.length == 0) {
                break;
            }else//添加符合条件的location进数组
                [arrayRanges addObject:[NSNumber numberWithInteger:rang1.location]];
        }
        return arrayRanges;
     }
     return nil;
}

- (NSDictionary *)dictionaryWithUrlString{
    if (self && self.length && [self rangeOfString:@"?"].length == 1) {
        NSArray *array = [self componentsSeparatedByString:@"?"];
        if (array && array.count == 2) {
            NSString *paramsStr = array[1];
            if (paramsStr.length) {
                NSMutableDictionary *paramsDict = [NSMutableDictionary dictionary];
                NSArray *paramArray = [paramsStr componentsSeparatedByString:@"&"];
                for (NSString *param in paramArray) {
                    if (param && param.length) {
                        NSArray *parArr = [param componentsSeparatedByString:@"="];
                        if (parArr.count == 2) {
                            [paramsDict setObject:parArr[1] forKey:parArr[0]];
                        }
                    }
                }
                return paramsDict;
            }else{
                return nil;
            }
        }else{
            return nil;
        }
    }else{
        return nil;
    }
}

- (u_int64_t)lf_fileSize
{
    signed long long fileSize = 0;
    NSFileManager *fileManager = [NSFileManager new]; // default is not thread safe
    if ([fileManager fileExistsAtPath:self]) {
        NSError *error = nil;
        NSDictionary *fileDict = [fileManager attributesOfItemAtPath:self error:&error];
        if (!error && fileDict) {
            fileSize = [fileDict fileSize];
        }
    }
    return fileSize;
}
@end
